 /**
 * Copyright 2010 Marc-Andre Brien, Andrew Giebfried, Josh Ivany, Mitchell
 * Caulk, Mike Noseworthy
 * 
 * This file is part of ctf2010-jammm.
 * 
 * ctf2010-jammm is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * ctf2010-jammm is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * ctf2010-jammm. If not, see <http://www.gnu.org/licenses/>.
 */

package networking;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class Controller {

	private Server playerServer;
	public static final String m_name = "Team1";
	private Socket sock;
	
	
	//temp remove/ make testcase
	public static void main(String[]args){
		new Controller(Server.RED).connect("localhost");
	}
	
	
	/**
	 * 
	 * Creates a new controller.
	 * 
	 * @param c - the player's Server associated with this controller
	 */
	public Controller(Server c){
		playerServer = c;
	}
	
	public void connect(String simulatorHost){
		
		try {
			sock = new Socket(simulatorHost,Server.getPortNumber(playerServer));
			BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
			
			// step 1: send "CTF2010 <Server>" to simulator
			bw.write("CTF2010 " + m_name);
			bw.newLine();
			bw.flush();
			
			//step 2: wait for simulator to respond with "CTF2010"
			String lineIn = br.readLine();
			if(!lineIn.equals("CTF2010")) {
				protocolViolated("Simulator didn't say CTF2010. Fail.");
			}
			System.out.println("Server check passed"); //testcaserize
			//step 3: send "Steal <Version>"
			bw.write("STEAL 7.0");
			bw.newLine();
			bw.flush();
			
			//step4: wait for simulator to respond with "STEAL 7.0"
			lineIn=br.readLine();
			if (!lineIn.equals("STEAL 7.0")){
				protocolViolated("Simulator didn't say STEAL 7.0. Fail.");
			}
			System.out.println("version check passed"); //testcaserize 
			
			
			System.out.println("end of function passed"); //testcaserize
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}

	private void protocolViolated(String string) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	
	
}
